"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "HYD" + formatDate(new Date(), "yyyyMMdd");

describe("meetingRecord", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })

        // 创建一个职工
        var result = httpPost("POST", "/v1.0/api/user/Create", {
            "job_number": "001",
            "name": "测试职工一",
            "gender": "男",
            "phone": "18300000002",
            "role": "职工",
            "region": "所在区域",
            "image": "",
            "company": "公司",
            "position": "职务",
        })

        //创建会议室
        result = httpPost("POST", "/v1.0/api/meetingroom/Create", {
            "name": "会议室101",
            "load": 20,
            "building": "中心楼",
            "floor": 1,
            "device": "白板,空调,会议桌椅",
            "remark": "隔音不好",
        })
    });

    after(function() {
        clearDB();
    });

    it("创建会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "该时间段会议室已被预约过");

        result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 14:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);
    });

    it("会议预约单详情", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.no, str_prefix + "001");
        assert.equal(result.data.status, "待审核");
        assert.equal(result.data.created_name, "企业管理员");
        assert.equal(result.data.created_phone, "18888888888");
        assert.equal(result.data.start_date_time, "2099-12-15 10:00");
        assert.equal(result.data.end_date_time, "2099-12-15 12:00");
        assert.equal(result.data.meetingroom_no, "HYS001");
        assert.equal(result.data.meetingroom_name, "会议室101");
        assert.equal(result.data.meetingroom_load, 20);
        assert.equal(result.data.meetingroom_status, "空闲");
        assert.equal(result.data.meetingroom_building, "中心楼");
        assert.equal(result.data.meetingroom_floor, 1);
        assert.equal(result.data.meetingroom_device[0].enu_value, "白板");
        assert.equal(result.data.meetingroom_device[1].enu_value, "空调");
        assert.equal(result.data.meetingroom_device[2].enu_value, "会议桌椅");
        assert.equal(result.data.meetingroom_remark, "隔音不好");
        assert.equal(result.data.meeting_title, "例行周会");
        assert.equal(result.data.meeting_content, "各抒己见");
        assert.equal(result.data.user_number, 10);
        assert.equal(result.data.reviewed_name, "企业管理员");
        assert.equal(result.data.operated_by, 0);
        assert.equal(result.data.operated_name, "");
    });

    it("编辑会议预约单 - 改变审批记录中的审核人", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Update", {
            "id": 1,
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 9:00",
            "end_date_time": "2099-12-15 11:00",
            "meeting_title": "例行周会2",
            "meeting_content": "各抒己见2",
            "user_number": 11,
            "reviewed_by": 2,
        })
        assert.equal(result.code, "200");

        var result = httpPost("POST", "/v1.0/api/meetingRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.start_date_time, "2099-12-15 09:00");
        assert.equal(result.data.end_date_time, "2099-12-15 11:00");
        assert.equal(result.data.meetingroom_no, "HYS001");
        assert.equal(result.data.meetingroom_name, "会议室101");
        assert.equal(result.data.meeting_title, "例行周会2");
        assert.equal(result.data.meeting_content, "各抒己见2");
        assert.equal(result.data.user_number, 11);
        assert.equal(result.data.reviewed_name, "测试职工一");
        assert.equal(result.data.operated_by, 0);
        assert.equal(result.data.operated_name, "");

        // 审批中审批人发生改变,审批列表中记录减少 (不是待我审批的)
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/meetingRecord/Update", {
            "id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.msg, "该时间段会议室已被预约过");

        result = httpPost("POST", "/v1.0/api/meetingRecord/Update", {
            "id": 1,
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.msg, "会议结束时间必须大于开始时间");

        result = httpPost("POST", "/v1.0/api/meetingRecord/Update", {
            "id": 1,
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会2",
            "meeting_content": "各抒己见2",
            "user_number": 11,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");

        // 审批中审批人发生改变,审批列表中记录增加 (是待我审批的)
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
    })

    it("通过会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("已审核的会议预约单不允许编辑", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Update", {
            "id": 1,

        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "会议预约已通过,不可再次编辑");
    })

    it("拒绝会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Refuse", {
            "id": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("撤回会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Withdraw", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("会议预约单列表-状态查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "status": "全部",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "status": "已撤回",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].operated_by, 1);
        assert.equal(result.data.list[0].operated_name, "企业管理员");

        result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "status": "已通过",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("会议预约单列表-申请人模糊查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "created_name": "企业",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        var result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "created_name": "啦啦啦",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("会议预约单列表-申请人模糊查询-统计", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Static", {
            "created_name": "企业",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 2);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 0);
        assert.equal(result.data.refused, 1);
        assert.equal(result.data.withdrawed, 1);

        var result = httpPost("POST", "/v1.0/api/meetingRecord/Static", {
            "created_name": "啦啦啦",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 0);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 0);
        assert.equal(result.data.refused, 0);
        assert.equal(result.data.withdrawed, 0);
    });

    it("会议预约单列表-时间区间查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 15:00",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 15:00",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
    });

    it("公务车管理-用车记录-会议室ID查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/List", {
            "meetingroom_id": 1,
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
    });

    it("创建/编辑会议预约单-选择所需会议室", function() {
        // 占用会议室
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 3);

        // 创建时-选择会议室结果

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

        // 编辑时-选择会议室结果 (排除当前会议预约单)

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "meeting_record_id": 3,
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "meeting_record_id": 3,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/meetingroom/EffectiveList", {
            "meeting_record_id": 3,
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

    });

    // it("过期预约-不可审核", function() {
    //     var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
    //         "meetingroom_id": 1,
    //         "start_date_time": "2009-12-15 10:00",
    //         "end_date_time": "2009-12-15 12:00",
    //         "meeting_title": "例行周会",
    //         "meeting_content": "各抒己见",
    //         "user_number": 10,
    //         "reviewed_by": 1,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);

    //     result = httpPost("POST", "/v1.0/api/meetingRecord/Get", {
    //         "id": 4,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);
    //     assert.equal(result.data.status, "待审核");
    //     assert.equal(result.data.meeting_reminder, "");

    //     result = httpPost("POST", "/v1.0/api/meetingRecord/Pass", {
    //         "id": 4
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "会议预约已过期");

    //     result = httpPost("POST", "/v1.0/api/meetingRecord/Refuse", {
    //         "id": 4
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "会议预约已过期");
    // });

});

require('test').run();